package com.sz.app.oss.api.trade;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.app.oss.exception.ErrorCodes;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.trade.constants.TrdConstants;
import com.sz.biz.trade.ord.dto.*;
import com.sz.biz.trade.ord.entity.TrdOrder;
import com.sz.biz.trade.ord.entity.TrdOrderStatus;
import com.sz.biz.trade.ord.entity.TrdOrderType;
import com.sz.biz.trade.ord.service.*;
import com.sz.common.base.constants.TemplateConstants;
import com.sz.common.base.dto.CommonModel;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.BizException;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.utils.DateUtils;
import com.sz.common.base.utils.HtmlUtil;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.system.entity.SysDict;
import com.sz.common.core.system.service.SysDictService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Function: 订单管理 <br>
 * Author: jifeixing <br>
 * Date: 2017-07-15 14:18:00
 */
@RestController
@RequestMapping("${api}/trd/order")
@Api(description = " ", tags = "043、贸易-订单管理")
public class TrdOrderController extends BaseController {

	@Autowired
	private TrdOrderService trdOrderService;

	@Autowired
	private TrdOrderConfirmationService trdOrderConfirmationService;

	@Autowired
	private SysDictService dictService;

	@Autowired
	private TrdOrderDeliveryService trdOrderDeliveryService;

	@Autowired
	private TrdOrderFileRelService trdOrderFileRelService;

	@Autowired
	private TrdOrderGoodsRelService trdOrderGoodsRelService;

	/**
	 * 所有订单类型
	 *
	 * @return
	 */
	@ApiOperation(value = "所有订单类型", notes = "所有订单类型")
	@RequestMapping(value = "/orderType", method = RequestMethod.GET)
	public ResultDto orderType() {
		List<CommonModel> list = trdOrderService.orderType();
		ResultDto result = new ResultDto();
		result.setData(list);
		return result;
	}

	/**
	 * 所有订单状态
	 *
	 * @return
	 */
	@ApiOperation(value = "所有订单状态", notes = "所有订单状态")
	@RequestMapping(value = "/orderStatus", method = RequestMethod.GET)
	public ResultDto orderStatus() {
		List<CommonModel> list = trdOrderService.orderStatus();
		ResultDto result = new ResultDto();
		result.setData(list);
		return result;
	}


	/**
	 * 根据订单号查询
	 */
	@ApiOperation(value = "根据订单号查询", notes = "根据订单号查询")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orderNo", value = "订单编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "customerType", value = "1: 客户； 2： 供应商", dataType = "string", paramType = "query") })
	@RequestMapping(value = "/orderNo", method = { RequestMethod.GET })
	public ResultDto findByOrderNo(@RequestParam(required = true) String orderNo, @RequestParam(required = true) String customerType) {
		TrdOrderInfoDto orderInfo;
		// 销售订单（客户订单查询）
		if (customerType.equals(TrdOrderType.SALE.getCode())) {
			orderInfo = trdOrderService.findByOrderNoSale(orderNo);
		} else { // 采购订单（供应商订单查询）
			orderInfo = trdOrderService.findByOrderNoPurchaser(orderNo);
		}
		ResultDto dto = new ResultDto();
		dto.setData(orderInfo);
		return dto;
	}

	/**
	 * 根据订单号查询商品信息
	 */
	@ApiOperation(value = "根据订单号查询商品信息", notes = "根据订单号查询商品信息")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orderNo", value = "订单编号", dataType = "string", paramType = "query")})
	@RequestMapping(value = "/goods", method = { RequestMethod.GET })
	public ResultDto findGoodsByOrderNo(@RequestParam(required = true) String orderNo) {
		ResultDto dto = new ResultDto();
		TrdOrder trdOrder = trdOrderService.findFullInfoByOrderNo(orderNo);
		List <TrdOrderGoodsRelDto> trdOrderGoodsRelDtos = trdOrderGoodsRelService.findByOrderNo(orderNo , 1);
		for(TrdOrderGoodsRelDto trdOrderGoodsRelDto : trdOrderGoodsRelDtos
				){
			if(null != trdOrderGoodsRelDto.getMinLimit() && null != trdOrderGoodsRelDto.getMaxLimit()){
				trdOrderGoodsRelDto.setGoodsNum(trdOrderGoodsRelDto.getMinLimit());
			}
			if(null != trdOrderGoodsRelDto.getMinLimit() && null == trdOrderGoodsRelDto.getMaxLimit()){
				trdOrderGoodsRelDto.setGoodsNum(trdOrderGoodsRelDto.getMinLimit());
			}
			if(null == trdOrderGoodsRelDto.getMinLimit() && null != trdOrderGoodsRelDto.getMaxLimit()){
				trdOrderGoodsRelDto.setGoodsNum(trdOrderGoodsRelDto.getMaxLimit());
			}
		}
		if(trdOrder == null){
			throw Exceptions.bizException(ErrorCodes.ERROR_COMMON , "订单不存在");
		}
		
		HashMap <String, Object> data = new HashMap <>();
		data.put("projectId" , trdOrder.getProjectId() == null ? "" : trdOrder.getProjectId());
		if(trdOrder.getOrderType().equals(TrdOrderType.SALE)){
			data.put("purchaserId" , trdOrder.getCustomerId());
		}
		else{
			data.put("purchaserId" , trdOrder.getPurchaserId());
		}
		data.put("sellId" , trdOrder.getSellerId() == null ? "" : trdOrder.getSellerId());
		data.put("currencyUnit" , trdOrder.getCurrencyCode() == null ? "" : trdOrder.getCurrencyCode());
		data.put("orderGoods" , trdOrderGoodsRelDtos);
		dto.setData(data);
		return dto;
	}

	/**
	 * 根据订单号删除
	 */
	@ApiOperation(value = "根据订单号删除", notes = "根据订单号删除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderNos", value = "订单号数组[]", required = true, dataType = "array", paramType = "body") })
    @RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_ORDER, Permissions.BIZ_MGT_TRD_ORDER_PURCHASE},logical = Logical.OR)
	@RequestMapping(value = "/delete", method = { RequestMethod.POST })
	public ResultDto batchDeleteByOrderNos(@RequestBody List<String> orderNos) {
		trdOrderService.batchDeleteByOrderNos(orderNos, 2);
		return getDeleteSuccessDto(MessagesCodes.MODULE_TRADE_ORDER);
	}

    /**
     * 批量提交订单
     */
    @ApiOperation(value = "批量提交订单", notes = "批量提交订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNos", value = "订单号数组[]", required = true, dataType = "array", paramType = "body") })
    @RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_ORDER, Permissions.BIZ_MGT_TRD_ORDER_PURCHASE},logical = Logical.OR)
    @RequestMapping(value = "/submit", method = { RequestMethod.POST })
    public ResultDto submit(@RequestBody List<String> orderNos) {
        trdOrderService.batchUpdateOrderStatus(orderNos, TrdOrderStatus.CONFIRMING);
        return getOrderSubmitSuccessDto(MessagesCodes.MODULE_TRADE_ORDER);
    }

    /**
     * 批量确认订单
     */
    @ApiOperation(value = "批量确认订单", notes = "批量确认订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNos", value = "订单号数组[]", required = true, dataType = "array", paramType = "body") })
    @RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_ORDER, Permissions.BIZ_MGT_TRD_ORDER_PURCHASE},logical = Logical.OR)
    @RequestMapping(value = "/confirm", method = { RequestMethod.POST })
    public ResultDto confirm(@RequestBody List<String> orderNos) {
        trdOrderService.batchUpdateOrderStatus(orderNos, TrdOrderStatus.DELIVERY);
        return getConfirmationSuccessDto(MessagesCodes.MODULE_TRADE_ORDER);
    }

    /**
     * 批量发货
     */
    @ApiOperation(value = "批量发货", notes = "批量发货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNos", value = "订单号数组[]", required = true, dataType = "array", paramType = "body") })
    @RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_ORDER, Permissions.BIZ_MGT_TRD_ORDER_PURCHASE},logical = Logical.OR)
    @RequestMapping(value = "/delivery", method = { RequestMethod.POST })
    public ResultDto delivery(@RequestBody List<String> orderNos) {
        trdOrderService.batchUpdateOrderStatus(orderNos, TrdOrderStatus.TRANSPORT);
        return getDeliverySuccessDto(MessagesCodes.MODULE_TRADE_ORDER);
    }

	/**
	 * 编辑订单
	 *
	 * @throws Exception
	 */
	@ApiOperation(value = "编辑订单", notes = "编辑订单<br>权限：" + Permissions.BIZ_MGT_TRD_ORDER)
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
    @RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_ORDER, Permissions.BIZ_MGT_TRD_ORDER_PURCHASE},logical = Logical.OR)
	public ResultDto update(@RequestBody @ApiParam(value = "订单信息", required = true) @Validated TrdOrderDto trdOrderDto) throws ParseException {
		trdOrderService.update(trdOrderDto, 2);
		return this.getUpdateSuccessDto(MessagesCodes.MODULE_TRADE_ORDER);
	}

	/**
	 * 订单列表
	 */
	@ApiOperation(value = "订单列表", notes = "订单列表")
	@ApiImplicitParams({ @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "orderNo", value = "订单编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "projectId", value = "项目id", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "orderType", value = "订单类型", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "startTime", value = "起始时间(yyyy HH:mm:ss)", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "endTime", value = "结束时间(yyyy HH:mm:ss)", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "goodsTypeId", value = "商品类型", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "goodsId", value = "商品Id", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "customerId", value = "客户id", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "cusUserId", value = "帐号id", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "orderStatus", value = "订单状态", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true") })
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	public ResultDto list(@RequestParam(required = false) String q,
                                       @RequestParam(required = false) String orderNo,
                                       @RequestParam(required = false) Integer projectId,
                                       @RequestParam(required = true) TrdOrderType orderType,
                                       @RequestParam(required = false) String startTime,
                                       @RequestParam(required = false) String endTime,
                                       @RequestParam(required = false) Integer goodsTypeId,
                                       @RequestParam(required = false) Integer goodsId,
                                       @RequestParam(required = false) String customerId,
                                       @RequestParam(required = false) String cusUserId,
                                       @RequestParam(required = false) TrdOrderStatus orderStatus,
                                       @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                       @RequestParam(required = false, defaultValue = "10") int pageSize,
                                       @RequestParam(required = false) String sort,
                                       @RequestParam(required = false, defaultValue = "true") Boolean asc) {
		q = StringUtils.replaceSqlPattern(q);
		QueryParams params = new QueryParams(pageSize, pageIndex, q, sort, asc);
		params.put("orderNo", orderNo);
		params.put("projectId", projectId);
		params.put("orderType", orderType);
		params.put("startTime", populateTime(startTime));
		params.put("endTime", populateTime(endTime));
		params.put("goodsTypeId", goodsTypeId);
		params.put("goodsId", goodsId);
		params.put("customerId", customerId);
		params.put("cusUserId", cusUserId);
		params.put("orderStatus", orderStatus);
		ResultDto dto;
		// 销售订单
		if (orderType.getCode().equals(TrdOrderType.SALE.getCode())) {
			dto = trdOrderService.findByFilterSale(params);
		} else { // 采购订单
			dto = trdOrderService.findByFilterPurchaser(params);
		}
		return dto;
	}

	/**
	 * 转换时间
	 *
	 * @param timeString
	 * @return
	 */
	private Date populateTime(String timeString) {
		Date date = null;
		try {
			if (!StringUtils.isEmpty(timeString)) {
				date = DateUtils.convertToDate(timeString, DateUtils.FORMAT_DETAIL);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 订单确认单
	 * 
	 * @throws Exception
	 */
	@ApiOperation(value = "订单确认单", notes = "订单确认单<br>权限：" + Permissions.BIZ_MGT_TRD_ORDER)
	@RequestMapping(value = "/save/confirmation", method = { RequestMethod.POST })
    @RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_ORDER, Permissions.BIZ_MGT_TRD_ORDER_PURCHASE},logical = Logical.OR)
	public ResultDto saveConfirmation(@RequestBody @ApiParam(value = "订单确认单信息", required = true) @Validated TrdOrderConfirmationSaveDto trdOrderConfirmationSaveDto) {
		trdOrderConfirmationService.save(trdOrderConfirmationSaveDto);
		return this.getSaveSuccessDto(MessagesCodes.MODULE_TRADE_ORDER);
	}

	

    /**
     * 下单
     * @throws Exception
     */
    @ApiOperation(value = "下单", notes = "下单<br>权限：" + Permissions.BIZ_MGT_TRD_ORDER)
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_ORDER, Permissions.BIZ_MGT_TRD_ORDER_PURCHASE},logical = Logical.OR)
    public ResultDto save(@RequestBody @ApiParam(value = "订单信息", required = true) @Validated TrdOrderDto trdOrderDto) throws ParseException {
        trdOrderDto.setOrderChannel(2);
        trdOrderService.save(trdOrderDto);
		ResultDto result = getSaveSuccessDto(com.sz.biz.common.constants.MessagesCodes.MODULE_TRADE_ORDER);
		TrdOrderStatus statusTo = trdOrderDto.getOrderStatus();
        if(null == statusTo){
            result = saveDraftSuccessDto();
        }else if(statusTo.getCode().equals(TrdOrderStatus.CONFIRMING.getCode())){
            result = getOrderSubmitSuccessDto(com.sz.biz.common.constants.MessagesCodes.MODULE_TRADE_ORDER);
        }
        return result;
    }


	/**
	 * 根据订单号查询订单确认单
	 */
	@ApiOperation(value = "根据订单号查询订单确认单", notes = "根据订单号查询订单确认单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderNo", value = "订单号 ", required = true, dataType = "string", paramType = "path") })
	@RequestMapping(value = "/confirmation/{orderNo}", method = { RequestMethod.GET })
	public ResultDto getConfirmationByOrder(@PathVariable String orderNo) {
		TrdOrderConfirmationInfoDto trdOrderConfirmationInfoDto = trdOrderService.getConfirmationByOrder(orderNo);
		ResultDto dto = new ResultDto();
		dto.setData(trdOrderConfirmationInfoDto);
		return dto;
	}


	/**
	 * 根据订单号查询收货确认单
	 */
	@ApiOperation(value = "根据订单号查询收货确认单", notes = "根据订单号查询收货确认单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderNo", value = "订单号 ", required = true, dataType = "string", paramType = "path") })
	@RequestMapping(value = "/delivery/{orderNo}", method = { RequestMethod.GET })
	public ResultDto getDeliveryByOrder(@PathVariable String orderNo) {
		TrdOrderDeliveryInfoDto trdOrderDeliveryInfoDto = trdOrderService.getDeliveryByOrder(orderNo);
		ResultDto dto = new ResultDto();
		dto.setData(trdOrderDeliveryInfoDto);
		return dto;
	}

	/**
	 * 根据订单号导出pdf文件
	 */
	@ApiOperation(value = "根据订单号导出pdf文件", notes = "根据订单号导出pdf文件")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderNo", value = "订单号 ", required = true, dataType = "string", paramType = "path"),
			@ApiImplicitParam(name = "type", value = "订单类型(1为订单确认单，2为订单收货单)", required = true, dataType = "String", paramType = "query") })
	@RequestMapping(value = "/export/pdf/{orderNo}", method = { RequestMethod.GET })
	public ResultDto exportPDF(@PathVariable String orderNo, @RequestParam(required = true) String type) {
		PathPDFDto pathPDFDto;
		if (TrdConstants.OrderType.CONFIRM.equals(type)) {
			pathPDFDto = trdOrderConfirmationService.findPathByOrderNo(orderNo);
		} else {
			pathPDFDto = trdOrderDeliveryService.findPathByOrderNo(orderNo);
		}
		ResultDto dto = new ResultDto();
		if (pathPDFDto != null) {
			dto.setData(pathPDFDto.getPath());
		}
		return dto;
	}

 

	/**
	 * 根据订单号导出pdf文件
	 */
	@ApiOperation(value = "根据订单号导出pdf文件", notes = "根据订单号导出pdf文件")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "orderNo", value = "订单号 ", required = true, dataType = "string", paramType = "query"),
		@ApiImplicitParam(name = "type", value = "订单类型(1为订单确认单，2为订单收货单)", required = true, dataType = "String", paramType = "query")
		})
	@RequestMapping(value = "/export/content", method = { RequestMethod.POST })
	public ResultDto createPDF(@RequestParam(required = true) String orderNo,@RequestParam(required = true) String type,@RequestBody @ApiParam(value = "html字符串", required = true) @Validated String contentHtml) {
		PDFDto pdfDto = trdOrderService.createPDFFile(type,orderNo,contentHtml);
        ResultDto result=null;
        if(TrdConstants.OrderType.CONFIRM.equals(type)){
            result= this.getGenerativeSuccessDto(MessagesCodes.MODULE_TRADE_ORDER_PDF_CREATE_SUCCESS);	
        }
        else{
        	 result= this.getGenerativeSuccessDto(MessagesCodes.MODULE_DELIVERY_ORDER_PDF_CREATE_SUCCESS);	
        }
        result.setData(pdfDto);
        return result;

	}

	/**
	 * 获取html模板内容
	 *
	 * @throws IOException
	 */
	@ApiOperation(value = "获取html模板内容", notes = "获取html模板内容")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "type", value = "订单类型(1为订单确认单，2为订单收货单)", required = true, dataType = "String", paramType = "query") 
	})
	@RequestMapping(value = "/html", method = { RequestMethod.GET })
	public ResultDto getHtml(@RequestParam(required = true) String type) throws IOException {
		Resource resource = new ClassPathResource(File.separator + "templates");
		String path = resource.getFile().getPath();
		if(TrdConstants.OrderType.CONFIRM.equals(type)){
			path = path + File.separator + TemplateConstants.TRD_CONFIRMATION;
		}
		else{
			path = path + File.separator + TemplateConstants.TRD_DELIVERY;
		}
		String content = HtmlUtil.readfile(path);
		ResultDto dto = new ResultDto();
		dto.setData(content);
		return dto;

	}


    /**
     * 订单收货确认单
     * @throws Exception
     */
    @ApiOperation(value = "订单收货确认单", notes = "订单收货确认单<br>权限：" + Permissions.BIZ_MGT_TRD_ORDER)
    @RequestMapping(value = "/save/delivery", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_ORDER, Permissions.BIZ_MGT_TRD_ORDER_PURCHASE},logical = Logical.OR)
    public ResultDto saveDelivery(@RequestBody @ApiParam(value = "订单确认单信息", required = true) @Validated TrdOrderDeliverySaveDto trdOrderDeliverySaveDto) {
        trdOrderDeliveryService.save(trdOrderDeliverySaveDto);
		return this.getSaveSuccessDto(MessagesCodes.MODULE_TRADE_ORDER);
    }

	/**
	 * 非SUNRUN订单收货确认单
	 * @throws Exception
	 */
	@ApiOperation(value = "非SUNRUN订单收货确认单", notes = "非SUNRUN订单收货确认单<br>权限：" + Permissions.BIZ_MGT_TRD_ORDER)
	@RequestMapping(value = "/save/delivery/normal",method = {RequestMethod.POST})
	@RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_ORDER, Permissions.BIZ_MGT_TRD_ORDER_PURCHASE},logical = Logical.OR)
	public ResultDto saveDeliveryNormal(@RequestBody @ApiParam(value = "非SUNRUN订单确认信息",required = true) @Validated NormalTrdOrderDeliverySaveDto normalTrdOrderDeliverySaveDto){
		trdOrderDeliveryService.saveNormal(normalTrdOrderDeliverySaveDto);
		return this.getOrderSubmitSuccessDto(MessagesCodes.MODULE_TRADE_ORDER);
	}
	/**
	 * 生成收货确认单PDF
	 * 
	 * @throws IOException
	 * 
	 * @throws Exception
	 */
	@ApiOperation(value = "生成收货确认单PDF", notes = "生成收货确认单PDF<br>权限：")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "orderNo", value = "订单号 ", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "type", value = "订单类型(1为订单确认单，2为订单收货单)", required = true, dataType = "String", paramType = "query") }) 
	@RequestMapping(value = "/export/base64code", method = { RequestMethod.POST })
	public ResultDto exportPDF(@RequestParam(required = true) String orderNo,@RequestParam(required = true) String type,@RequestBody @ApiParam(value = "html字符串", required = true) @Validated String image) throws IOException {
		String path=trdOrderService.exportPDF(image,type,orderNo);
		ResultDto dto = new ResultDto();
		dto.setData(path);
		return dto;
	}
	/**
	 * 根据订单号查询收货确认单
	 */
	@ApiOperation(value = "根据订单号查询收货确认单", notes = "根据订单号查询收货确认单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderNo", value = "订单号 ", required = true, dataType = "string", paramType = "path"),
	        @ApiImplicitParam(name = "type", value = "订单类型(1为订单确认单，2为订单收货单)", required = true, dataType = "String", paramType = "query") }) 
	@RequestMapping(value = "/{orderNo}", method = { RequestMethod.GET })
	public ResultDto getDataByOrder(@PathVariable String orderNo, @RequestParam(required = true) String type) {
	    ResultDto result = this.getSaveSuccessDto(MessagesCodes.MODULE_TRADE_ORDER_PDF_CREATE_SUCCESS);
		if(TrdConstants.OrderType.CONFIRM.equals(type)){
			TrdOrderConfirmationInfoDto trdOrderConfirmationInfoDto=trdOrderConfirmationService.findByOrderNo(orderNo);
			result.setData(trdOrderConfirmationInfoDto);
		} else {
			TrdOrderDeliveryInfoDto trdOrderDeliveryInfoDto = trdOrderService.getDeliveryByOrder(orderNo);
			result.setData(trdOrderDeliveryInfoDto);
		}		
		return result;
	}

	/**
	 * 订单收货确认单
	 * @throws Exception
	 */
	@ApiOperation(value = "非SUNRUN订单收货确认", notes = "订单收货确认<br>权限：" + Permissions.BIZ_MGT_TRD_ORDER)
	@RequestMapping(value = "/signed", method = {RequestMethod.POST})
	@RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_ORDER, Permissions.BIZ_MGT_TRD_ORDER_PURCHASE},logical = Logical.OR)
	public ResultDto received(@RequestBody List<String> orderNos) {
		trdOrderService.signed(orderNos);
		return this.getOperateSuccessDto(MessagesCodes.MODULE_TRADE_ORDER_SIGNED_SUCCESS);
	}


	@ApiOperation(value = "获取贸易订单文件类型列表", notes = "返回贸易订单文件类型列表")
	@RequestMapping(value = "/file/type", method = {RequestMethod.GET})
	public
	@ResponseBody
	ResultDto getTrdOrderFileTypes() throws BizException {
		List<SysDict> dictList = dictService.findByCatalog("trd.order.file.type");
		ResultDto dto = new ResultDto();
		dto.setData(dictList);
		return dto;
	}

}
